import java.util.Arrays;

public class TestDemo3 {

    // 数组转字符串
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,7};
        String str = myToString(arr);
        System.out.println(str);
    }

    public static String myToString(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str += arr[i];
            if (i != arr.length-1) {
                str += ", ";
            }
        }
        str += "]";
        return str;
    }

    // 数组的拷贝
    public static void main6(String[] args) {

        int[] arr = {1,2,3,4,5,7};
        int[] copy = copyOf(arr);
        System.out.println(Arrays.toString(copy));
    }

    public static int[] copyOf(int[] arr) {
        int[] copy = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            copy[i] = arr[i];
        }
        return copy;
    }

    // 创建的数组，并且赋初始值
    public static void main5(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = i+1;
        }
    }

    // 打印数组
    public static void main4(String[] args) {
        int[] arr = {1,2,3,4,5,7};
        printArray(arr);
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // 改变原有数组元素的值
    public static void main3(String[] args) {
        int[] arr = {1,2,3,4,5,7};
        transform(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void transform(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i]*2;
        }
    }

    // 数组所有元素之和
    public static void main2(String[] args) {
        int[] arr = {1,2,3,4,5,7};
        System.out.println(sum(arr));
    }

    public static int sum(int[] arr) {
        int add = 0;
        for (int i = 0; i < arr.length; i++) {
            add += arr[i];
        }
        return add;
    }

    // 求数组的平均值
    public static void main1(String[] args) {
        int[] arr = {1,2,3,4,5,7};
        System.out.println(avg(arr));
    }

    public static double avg(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return (sum*1.0/ arr.length);
    }
}
